<template>
  <el-dialog :title="$t('addCommunity.title')" :visible.sync="visible" width="40%" @close="clearAddCommunityInfo">
    <el-form ref="form" :model="addCommunityInfo" label-width="120px" class="text-left">
      <el-form-item :label="$t('addCommunity.name')" prop="name" required>
        <el-input v-model="addCommunityInfo.name" :placeholder="$t('addCommunity.namePlaceholder')" />
      </el-form-item>
      <el-form-item :label="$t('addCommunity.area')" prop="area">
        <area-select @selectArea="selectArea" />
      </el-form-item>
      <el-form-item :label="$t('addCommunity.address')" prop="tmpAddress" required>
        <el-input v-model="addCommunityInfo.tmpAddress" :placeholder="$t('addCommunity.addressPlaceholder')" />
      </el-form-item>
      <el-form-item :label="$t('addCommunity.nearbyLandmarks')" prop="nearbyLandmarks" required>
        <el-input v-model="addCommunityInfo.nearbyLandmarks"
          :placeholder="$t('addCommunity.nearbyLandmarksPlaceholder')" />
      </el-form-item>
      <el-form-item :label="$t('addCommunity.tel')" prop="tel" required>
        <el-input v-model="addCommunityInfo.tel" :placeholder="$t('addCommunity.telPlaceholder')" />
      </el-form-item>
      <el-form-item :label="$t('addCommunity.payFeeMonth')" prop="payFeeMonth" required>
        <el-input v-model="addCommunityInfo.payFeeMonth" :placeholder="$t('addCommunity.payFeeMonthPlaceholder')" />
      </el-form-item>
      <el-form-item :label="$t('addCommunity.feePrice')" prop="feePrice" required>
        <el-input v-model="addCommunityInfo.feePrice" :placeholder="$t('addCommunity.feePricePlaceholder')" />
      </el-form-item>
      <el-form-item v-for="(item, index) in addCommunityInfo.attrs" :key="index" :label="item.specName"
        :prop="'attrs.' + index + '.value'">
        <el-input v-if="item.specType === '2233'" v-model="item.value" :placeholder="item.specHoldplace" />
        <el-select v-else-if="item.specType === '3344'" v-model="item.value" :placeholder="item.specHoldplace"
          style="width: 100%">
          <el-option v-for="value in item.values" :key="value.value" :label="value.valueName" :value="value.value" />
        </el-select>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="visible = false">{{ $t('common.cancel') }}</el-button>
      <el-button type="primary" @click="saveCommunityInfo">{{ $t('common.save') }}</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { saveCommunity } from '@/api/community/communityManageApi'
import { getAttrSpecList } from '@/api/dev/attrSpecApi'
import { getAttrValueList } from '@/api/dev/attrValueApi'
import AreaSelect from '@/components/community/areaSelect'

export default {
  name: 'AddCommunity',
  data() {
    return {
      visible: false,
      addCommunityInfo: {
        name: '',
        address: '',
        tmpAddress: '',
        areaAddress: '',
        nearbyLandmarks: '',
        tel: '',
        cityCode: '',
        mapX: '101.33',
        mapY: '101.33',
        attrs: [],
        payFeeMonth: 12,
        feePrice: 0
      }
    }
  },
  components: { AreaSelect },
  methods: {
    openModal() {
      this._loadCommunityAttrSpec()
      this.visible = true
    },
    selectArea(area) {
      this.addCommunityInfo.cityCode = area.selectArea
    },
    saveCommunityInfo() {
      this.addCommunityInfo.address = this.addCommunityInfo.areaAddress + this.addCommunityInfo.tmpAddress
      if (!this.validateForm()) {
        return
      }
      saveCommunity(this.addCommunityInfo).then(res => {
        console.log(res)
        this.$message.success(this.$t('addCommunity.saveSuccess'))
        this.visible = false
        this.$emit('listData')
      }).catch(error => {
        this.$message.error(error.message)
      })
    },
    validateForm() {
      if (!this.addCommunityInfo.name) {
        this.$message.error(this.$t('addCommunity.validate.name'))
        return false
      }
      if (!this.addCommunityInfo.cityCode) {
        this.$message.error(this.$t('addCommunity.validate.cityCode'))
        return false
      }
      if (!this.addCommunityInfo.address) {
        this.$message.error(this.$t('addCommunity.validate.address'))
        return false
      }
      if (!this.addCommunityInfo.nearbyLandmarks) {
        this.$message.error(this.$t('addCommunity.validate.nearbyLandmarks'))
        return false
      }
      if (!this.addCommunityInfo.tel) {
        this.$message.error(this.$t('addCommunity.validate.tel'))
        return false
      }
      return true
    },
    clearAddCommunityInfo() {
      this.addCommunityInfo = {
        name: '',
        address: '',
        tmpAddress: '',
        areaAddress: '',
        nearbyLandmarks: '',
        cityCode: '',
        mapX: '101.33',
        mapY: '101.33',
        attrs: [],
        payFeeMonth: 12,
        feePrice: 0
      }
    },
    async _loadCommunityAttrSpec() {
      this.addCommunityInfo.attrs = []
      const { data } = await getAttrSpecList({
        page: 1,
        row: 100,
        tableName: 'building_community_attr'
      })
      data.forEach(item => {
        item.value = ''
        if (item.specShow === 'Y') {
          item.values = []
          this._loadAttrValue(item.specCd, item.values)
          this.addCommunityInfo.attrs.push(item)
        }
      })
    },
    async _loadAttrValue(specCd, values) {
      const { data } = await getAttrValueList({ specCd, page: 1, row: 100 })
      data.forEach(item => {
        if (item.valueShow === 'Y') {
          values.push(item)
        }
      })

    }
  }
}
</script>